﻿Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Serialization
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.SharePoint.Publishing

'Description of the tool part. Override the GetToolParts method in your WebPart
'class to invoke this tool part. To establish a reference to the Web Part 
'the user has selected, use the ParentToolPane.SelectedWebPart property.
Public Class MapsToolPart
    Inherits Microsoft.SharePoint.WebPartPages.ToolPart


    Dim WithEvents txtListLocation As TextBox = New TextBox
    Dim WithEvents txtList As TextBox = New TextBox
    Dim cmsImage As Publishing.WebControls.AssetUrlSelector = New Publishing.WebControls.AssetUrlSelector
    Dim txtImage As TextBox = New TextBox
    Dim txtKey As TextBox = New TextBox
    Dim btnBrowse As Button = New Button
    Dim ddlTitle As DropDownList = New DropDownList
    Dim ddlLong As DropDownList = New DropDownList
    Dim ddlLat As DropDownList = New DropDownList
    Dim ddlPicture As DropDownList = New DropDownList
    Dim ddlDescription As DropDownList = New DropDownList
    Dim ddlValues As DropDownList = New DropDownList
    Dim ddlZoom As DropDownList = New DropDownList
    Dim ddlMapType As DropDownList = New DropDownList
    Dim l As New HyperLink


    Dim txtTitle As TextBox = New TextBox
    Dim txtLong As TextBox = New TextBox
    Dim txtLat As TextBox = New TextBox
    Dim txtSingleImage As TextBox = New TextBox
    Dim cmsSingleImage As Publishing.WebControls.AssetUrlSelector = New Publishing.WebControls.AssetUrlSelector


    Sub New()
        Me.Title = "Map Options"
    End Sub

    Public Overrides Sub ApplyChanges()
        Dim MapsWebPart As MJB.Sharepoint.GoogleMaps.MapsWebPart = CType(Me.ParentToolPane.SelectedWebPart, MJB.Sharepoint.GoogleMaps.MapsWebPart)
        MapsWebPart.the_site = txtListLocation.Text
        MapsWebPart.the_list = txtList.Text
        MapsWebPart.fieldTitle = ddlTitle.SelectedValue
        MapsWebPart.fieldLong = ddlLong.SelectedValue
        MapsWebPart.fieldLat = ddlLat.SelectedValue
        MapsWebPart.defaultImageURL = txtImage.Text
        MapsWebPart.fieldImage = ddlPicture.SelectedValue
        MapsWebPart.fieldDescription = ddlDescription.SelectedValue()
        MapsWebPart.singleTitle = txtTitle.Text
        MapsWebPart.singleLong = txtLong.Text
        MapsWebPart.singleLat = txtLat.Text
        MapsWebPart.singleImageURL = txtSingleImage.Text
        MapsWebPart.APIKey = txtKey.Text
        MapsWebPart.MapCentre = ddlValues.SelectedValue
        MapsWebPart.Zoom = ddlZoom.SelectedValue
        MapsWebPart.MapType = ddlMapType.SelectedValue
    End Sub

    'If the ApplyChanges method succeeds, this method is called by the ToolPane object
    'to refresh the specified property values in the tool part user interface.
    Public Overrides Sub SyncChanges()
    End Sub

    'This method is called by the ToolPane object if the user discards changes to the selected Web Part. 
    Public Overrides Sub CancelChanges()
    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        Microsoft.SharePoint.WebControls.ScriptLink.Register(Me.Page, "PickerTreeDialog.js", True)
        AddSitePickerControlClientScript()
        MyBase.OnPreRender(e)
    End Sub

    Private Sub AddSitePickerControlClientScript()


        Dim script As String
        script = "<script language=""javascript"">"
        script += "var lastSelectedSiteSmtPickerId = null; "
        script += "function LaunchSitePicker() "
        script += "{ "
        script += "if (!document.getElementById) return; "
        script += "var siteTextBox = document.getElementById('"
        script += SPHttpUtility.EcmaScriptStringLiteralEncode(Me.txtListLocation.ClientID)
        script += "'); "
        script += "var siteListBox = document.getElementById('"
        script += SPHttpUtility.EcmaScriptStringLiteralEncode(Me.txtList.ClientID)
        script += "'); "
        script += "if (siteTextBox == null) return; "
        script += "var serverUrl = '/'; "
        script += "var callback=''; "
        script += "var callback = function(results) "
        script += "{ "
        script += "if (results == null) return; "
        script += "lastSelectedSiteSmtPickerId = results[0]; "
        script += "siteTextBox.value = ''; "
        script += "siteListBox.value = '';"
        script += "siteTextBox.value = results[1]; "
        script += "siteListBox.value = results[2];"
        script += "}; "
        script += "LaunchPickerTreeDialog("
        script += "'CbqPickerSelectSiteTitle',"
        script += "'CbqPickerSelectSiteText', "
        script += "'websLists','', "
        script += "serverUrl, lastSelectedSiteSmtPickerId,"
        script += "'','','/_layouts/images/smt_icon.gif','', "
        script += "callback); "
        script += "}"
        script += "</script>"

        Me.Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "LanchSitePicker", script)
    End Sub


    'Render this tool part to the output parameter specified. 
    Protected Overrides Sub RenderToolPart(ByVal output As System.Web.UI.HtmlTextWriter)

        EnsureChildControls()

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Google Maps API Key")
        output.WriteBreak()
        l.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtKey.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Zoom Level")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlZoom.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Map Type")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlMapType.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "ms-TPTitle")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Using a List")

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Select the list where your building information is stored")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        btnBrowse.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Site")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtListLocation.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("List Name")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtList.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionTitle")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Picking fields from the list")

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Which field holds the Name of the Building?")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlTitle.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Which field holds the 'Longitude'?")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlLong.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Which field holds the 'Latitude'?")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlLat.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Which field holds the HTML description")
        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlDescription.RenderControl(output)

        output.WriteEndTag("div")




        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")




        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionTitle")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Images")

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Which field holds the URL of the image?")
        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlPicture.RenderControl(output)

        output.WriteEndTag("div")




        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Find a default image to be used if there is no image field or it is blank")
        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtImage.RenderControl(output)
        cmsImage.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionTitle")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Map Centre")

        output.WriteEndTag("div")



        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Pick a value from your list to centre the map on")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        ddlValues.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "ms-TPTitle")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Single Point")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Location's 'Longitude'")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtLong.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Location's 'Latitude'")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtLat.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Location's Title")

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtTitle.RenderControl(output)

        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")



        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionHead")
        output.Write(Html32TextWriter.TagRightChar)

        output.Write("Image URL")
        output.WriteEndTag("div")

        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserSectionBody")
        output.Write(Html32TextWriter.TagRightChar)

        txtSingleImage.RenderControl(output)
        cmsSingleImage.RenderControl(output)

        output.WriteEndTag("div")


        output.WriteBeginTag("div")
        output.WriteAttribute("class", "UserDottedLine")
        output.Write(Html32TextWriter.TagRightChar)
        output.WriteEndTag("div")




    End Sub


    Protected Sub txtListLocation_Changed(ByVal sender As Object, ByVal e As EventArgs) Handles txtList.TextChanged
        fieldList(ddlTitle)
        fieldList(ddlLong)
        fieldList(ddlLat)
        fieldList(ddlPicture)
        fieldList(ddlDescription)
        valueList(ddlValues)
    End Sub



    Protected Overrides Sub CreateChildControls()


        Dim MapsWebPart As MJB.Sharepoint.GoogleMaps.MapsWebPart = CType(Me.ParentToolPane.SelectedWebPart, MJB.Sharepoint.GoogleMaps.MapsWebPart)

        l.Text = "Register for a Google Maps API Key in a New Window"
        l.NavigateUrl = "http://code.google.com/apis/maps/signup.html"
        l.ToolTip = "Google Maps API Key"
        l.Target = "_new"
        Controls.Add(l)

        btnBrowse.Attributes.Add("onClick", "LaunchSitePicker()")
        btnBrowse.Text = "Browse..."
        Controls.Add(btnBrowse)

        txtListLocation.AutoPostBack = True
        txtListLocation.Text = MapsWebPart.the_site
        Controls.Add(txtListLocation)

        txtList.AutoPostBack = True
        txtList.Text = MapsWebPart.the_list
        Controls.Add(txtList)

        If Not String.IsNullOrEmpty(MapsWebPart.the_list) Then
            fieldList(ddlTitle)
            fieldList(ddlLong)
            fieldList(ddlLat)
            fieldList(ddlPicture)
            fieldList(ddlDescription)
            valueList(ddlValues)
        End If

        If Not String.IsNullOrEmpty(MapsWebPart.fieldTitle) Then
            ddlTitle.Items.FindByValue(MapsWebPart.fieldTitle).Selected = True
        End If
        If Not String.IsNullOrEmpty(MapsWebPart.fieldLat) Then
            ddlLat.Items.FindByValue(MapsWebPart.fieldLat).Selected = True
        End If
        If Not String.IsNullOrEmpty(MapsWebPart.fieldLong) Then
            ddlLong.Items.FindByValue(MapsWebPart.fieldLong).Selected = True
        End If
        If Not String.IsNullOrEmpty(MapsWebPart.fieldImage) Then
            ddlPicture.Items.FindByValue(MapsWebPart.fieldImage).Selected = True
        End If
        If Not String.IsNullOrEmpty(MapsWebPart.fieldDescription) Then
            ddlDescription.Items.FindByValue(MapsWebPart.fieldDescription).Selected = True
        End If




        Controls.Add(ddlTitle)
        Controls.Add(ddlLong)
        Controls.Add(ddlLat)
        Controls.Add(ddlPicture)
        Controls.Add(ddlDescription)

        txtImage.Width = System.Web.UI.WebControls.Unit.Pixel(80)
        txtImage.Text = MapsWebPart.defaultImageURL
        Controls.Add(txtImage)

        cmsImage.AssetUrlTextBoxVisible = False
        cmsImage.AssetUrlClientID = txtImage.ClientID
        cmsImage.PickerButtonText = "Find"
        Controls.Add(cmsImage)

        If Not String.IsNullOrEmpty(MapsWebPart.MapCentre) Then
            ddlValues.Items.FindByValue(MapsWebPart.MapCentre).Selected = True
        End If
        Controls.Add(ddlValues)

        txtKey.Text = MapsWebPart.APIKey
        Controls.Add(txtKey)

        txtLat.Text = MapsWebPart.singleLat
        Controls.Add(txtLat)

        txtLong.Text = MapsWebPart.singleLong
        Controls.Add(txtLong)

        txtTitle.Text = MapsWebPart.singleTitle
        Controls.Add(txtTitle)

        txtSingleImage.Width = System.Web.UI.WebControls.Unit.Pixel(80)
        txtSingleImage.Text = MapsWebPart.singleImageURL
        Controls.Add(txtSingleImage)

        cmsSingleImage.AssetUrlTextBoxVisible = False
        cmsSingleImage.AssetUrlClientID = txtSingleImage.ClientID
        cmsSingleImage.Width = System.Web.UI.WebControls.Unit.Pixel(80)
        cmsSingleImage.PickerButtonText = "Find"
        Controls.Add(cmsSingleImage)

        Dim newItem As ListItem
        newItem = New ListItem
        newItem.Text = "17 - Street Level"
        newItem.Value = "17"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "16"
        newItem.Value = "16"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "15"
        newItem.Value = "15"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "14 - Town Level"
        newItem.Value = "14"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "13"
        newItem.Value = "13"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "12"
        newItem.Value = "12"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "11 - City Level"
        newItem.Value = "11"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "10"
        newItem.Value = "10"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "9"
        newItem.Value = "9"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "8 - Region Level"
        newItem.Value = "8"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "7"
        newItem.Value = "7"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "6"
        newItem.Value = "6"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "5 - Country Level"
        newItem.Value = "5"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "4"
        newItem.Value = "4"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "3"
        newItem.Value = "3"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "2"
        newItem.Value = "2"
        ddlZoom.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "World Level"
        newItem.Value = "1"
        ddlZoom.Items.Add(newItem)



        newItem = New ListItem
        newItem.Text = "Normal Map"
        newItem.Value = "G_NORMAL_MAP"
        ddlMapType.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "Hybrid Map"
        newItem.Value = "G_HYBRID_MAP"
        ddlMapType.Items.Add(newItem)

        newItem = New ListItem
        newItem.Text = "Satellite Map"
        newItem.Value = "G_SATELLITE_MAP"
        ddlMapType.Items.Add(newItem)

        If Not String.IsNullOrEmpty(MapsWebPart.MapType) Then
            ddlMapType.Items.FindByValue(MapsWebPart.MapType).Selected = True
        End If
        If Not String.IsNullOrEmpty(MapsWebPart.Zoom) Then
            ddlZoom.Items.FindByValue(MapsWebPart.Zoom).Selected = True
        End If



        Controls.Add(ddlMapType)
        Controls.Add(ddlZoom)

        MyBase.CreateChildControls()

    End Sub


    Function fieldList(ByVal outgoingList As DropDownList) As DropDownList

        Dim field As SPField
        Dim incominglist As SPList = Microsoft.SharePoint.WebControls.SPControl.GetContextSite(Context).OpenWeb(txtListLocation.Text).Lists(txtList.Text)

        For Each field In incominglist.Fields

            Dim newItem As New ListItem
            newItem.Text = field.Title
            newItem.Value = field.Title
            outgoingList.Items.Add(newItem)
        Next

        Return outgoingList


    End Function

    Function valueList(ByVal outgoingList As DropDownList) As DropDownList

        Dim item As SPListItem
        Dim incominglist As SPList = Microsoft.SharePoint.WebControls.SPControl.GetContextSite(Context).OpenWeb(txtListLocation.Text).Lists(txtList.Text)

        For Each item In incominglist.Items

            Dim newItem As New ListItem
            newItem.Text = item.Title
            newItem.Value = item.ID
            outgoingList.Items.Add(newItem)
        Next

        Return outgoingList


    End Function


End Class

